<template>
  <el-container
    class="add-school-container"
    style="display: flex; flex-direction: column; height: 86vh"
  >
    <el-main style="flex-grow: 1">
      <el-card class="page-title-card">
        <template #header>
          <h1 class="page-title">新增学校</h1>
        </template>
        <el-form
          :model="schoolForm"
          :rules="rules"
          ref="schoolFormRef"
          label-width="120px"
          class="add-school-form"
        >
          <el-form-item label="学校名称" prop="name">
            <el-input
              v-model="schoolForm.name"
              placeholder="请输入学校名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input
              v-model="schoolForm.address"
              placeholder="请输入学校地址"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系人">
            <el-input
              v-model="schoolForm.contact"
              placeholder="请输入联系人姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input
              v-model="schoolForm.phone"
              placeholder="请输入联系电话"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(schoolFormRef)"
              >提交</el-button
            >
            <el-button @click="resetForm(schoolFormRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-main>
  </el-container>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const schoolForm = ref({
  name: "",
  address: "",
  contact: "",
  phone: "",
});

const rules = ref({
  name: [{ required: true, message: "请输入学校名称", trigger: "blur" }],
  address: [{ required: true, message: "请输入学校地址", trigger: "blur" }],
  phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
});

const schoolFormRef = ref(null);

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      console.log("提交成功！");
      // 提交数据到后端的逻辑
    } else {
      console.log("请检查输入信息！");
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style  scoped>
.add-school-container {
  display: flex;
  flex-direction: column;
}

.page-title-card {
  margin: 20px;
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.page-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 20px;
}

.add-school-form {
  padding: 20px;
}
</style>
